package com.shchuangzhi.smartguard.core;

import android.content.Context;
import android.os.Bundle;
import android.os.Environment;

import com.elvishew.xlog.XLog;
import com.iflytek.cloud.ErrorCode;
import com.iflytek.cloud.InitListener;
import com.iflytek.cloud.SpeechConstant;
import com.iflytek.cloud.SpeechError;
import com.iflytek.cloud.SpeechSynthesizer;
import com.iflytek.cloud.SynthesizerListener;

import java.util.Date;
import java.util.GregorianCalendar;

/**
 * Created by j_hu on 5/27/17.
 */

public class TTS {

    private static TTS instance;
    private SpeechSynthesizer speech;

    public TTS(Context ctx) {
        speech = SpeechSynthesizer.createSynthesizer(ctx, ttsInitListener);
        setParam();
    }

    public static void init(Context ctx) {
        if (instance == null) {
            instance = new TTS(ctx);
        }
    }

    public static TTS get() {
        return instance;
    }

    public String withTpl1(int state, String name, Date date) {
        GregorianCalendar c = new GregorianCalendar();
        c.setTime(date);
        if (state == Consts.IN_MODE) {
            return String.format("%s%s", c.get(GregorianCalendar.AM_PM) == GregorianCalendar.AM ? "早上好" : "下午好", name);
        } else {
            return String.format("再见%s", name);
        }
    }

    public void speak(String text) {
        if (null == speech) {
            XLog.e("error on tts init." );
            return;
        }

        if (text == null || text.equals("")) {
            return;
        }

        int code = speech.startSpeaking(text, ttsListener);
        if (code != ErrorCode.SUCCESS) {
            XLog.e("compose fail");
        }
    }

    private void setParam(){
        // 清空参数
        speech.setParameter(SpeechConstant.PARAMS, null);
        // 根据合成引擎设置相应参数
        speech.setParameter(SpeechConstant.ENGINE_TYPE, SpeechConstant.TYPE_CLOUD);
        // 设置在线合成发音人
        speech.setParameter(SpeechConstant.VOICE_NAME, "nannan");
        //设置合成语速
        speech.setParameter(SpeechConstant.SPEED, "90");
        //设置合成音调
        speech.setParameter(SpeechConstant.PITCH, "60");
        //设置合成音量
        speech.setParameter(SpeechConstant.VOLUME, "80");
        //设置播放器音频流类型
        speech.setParameter(SpeechConstant.STREAM_TYPE, "3");
        // 设置播放合成音频打断音乐播放，默认为true
        speech.setParameter(SpeechConstant.KEY_REQUEST_FOCUS, "true");

        // 设置音频保存路径，保存音频格式支持pcm、wav，设置路径为sd卡请注意WRITE_EXTERNAL_STORAGE权限
        // 注：AUDIO_FORMAT参数语记需要更新版本才能生效
        speech.setParameter(SpeechConstant.AUDIO_FORMAT, "wav");
        speech.setParameter(SpeechConstant.TTS_AUDIO_PATH, Environment.getExternalStorageDirectory()+"/smartguard/tts.wav");
    }

    private InitListener ttsInitListener = new InitListener() {
        @Override
        public void onInit(int code) {
            if (code != ErrorCode.SUCCESS) {
                XLog.e("tts init failed code = " + code);
            }
        }
    };

    private SynthesizerListener ttsListener = new SynthesizerListener() {
        @Override
        public void onSpeakBegin() {

        }

        @Override
        public void onSpeakPaused() {
        }

        @Override
        public void onSpeakResumed() {
        }

        @Override
        public void onBufferProgress(int percent, int beginPos, int endPos,
                                     String info) {
        }

        @Override
        public void onSpeakProgress(int percent, int beginPos, int endPos) {
        }

        @Override
        public void onCompleted(SpeechError error) {
        }

        @Override
        public void onEvent(int eventType, int arg1, int arg2, Bundle obj) {
        }
    };
}
